package org.jeecg.modules.station.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.RedisPublicKeyPrefixConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.station.entity.MtServiceStation;
import org.jeecg.modules.station.mapper.MtServiceStationMapper;
import org.jeecg.modules.station.service.IMtServiceStationService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 下属服务站
 * @Author: jeecg-boot
 * @Date: 2022-08-08
 * @Version: V1.0
 */
@Service
public class MtServiceStationServiceImpl extends ServiceImpl<MtServiceStationMapper, MtServiceStation> implements IMtServiceStationService {

    private final ISysUserService iSysUserService;
    private final RedisUtil redisUtil;
    private final ISysUserRoleService iSysUserRoleService;
    private final ISysRoleService sysRoleService;
    private final ISysUserService sysUserService;

    public MtServiceStationServiceImpl(ISysUserService iSysUserService, RedisUtil redisUtil, ISysUserRoleService iSysUserRoleService, ISysRoleService sysRoleService, ISysUserService sysUserService) {
        this.iSysUserService = iSysUserService;
        this.redisUtil = redisUtil;
        this.iSysUserRoleService = iSysUserRoleService;
        this.sysRoleService = sysRoleService;
        this.sysUserService = sysUserService;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveServiceStationAndCreateUser(MtServiceStation mtServiceStation) {
        JSONObject user = new JSONObject();
        user.put("username", mtServiceStation.getLinkPhone());//用户账号：联系电话
        user.put("realname", mtServiceStation.getStationName());//用户名称：服务站名称
        user.put("password", mtServiceStation.getLinkPhone()); //密码 ：服务站联系人手机号码
        user.put("phone", mtServiceStation.getLinkPhone()); //用户联系人电话：服务站联系人手机号码
        user.put("confirmpassword", mtServiceStation.getLinkPhone()); //确认密码
        user.put("useridentity", 1); //身份（1 普通成员 2 上级）
        user.put("userId", mtServiceStation.getUserId());
        user.put("activitisync", 1);//同步工作流引擎(1-同步,0-不同步)
        user.put("contacts", mtServiceStation.getLinkMan());//联系人
        user.put("workNo", mtServiceStation.getLinkPhone());//用户工号：服务站联系人手机号码
        user.put("usertype", 2);//0平台管理员 1服务商 2服务站 3工程师 4客户
        Result<SysUser> userResult = iSysUserService.addUserAndDept(user, CommonConstant.ROLE_SERVICE_STATION);
        redisUtil.removeAll(CacheConstant.SYS_DEPARTS_CACHE);
        redisUtil.removeAll(CacheConstant.SYS_DEPART_IDS_CACHE);
        if (!userResult.isSuccess()) {
            //手动报错 强制事务回滚
            throw new JeecgBootException(userResult.getMessage());
        }
        SysUser result = userResult.getResult();
        mtServiceStation.setUserId(result.getUsername()) //绑定用户
                .setSysOrgCode(result.getOrgCode()); //当前生成部门
        super.save(mtServiceStation);
        // 更新用户关联id
        SysUser sysUser = new SysUser();
        sysUser.setId(result.getId())
                .setLinkId(mtServiceStation.getId());
        sysUserService.updateById(sysUser);
        String string = JSON.toJSONString(mtServiceStation);
        redisUtil.set(RedisPublicKeyPrefixConstant.STATION + ":" + mtServiceStation.getSysOrgCode() + ":" + mtServiceStation.getCreateBy(), string, 60 * 60 * 2);
    }

    @Override
    public void deleteServiceStationAndUser(MtServiceStation mtServiceStation) {

    }

}
